package com.cby.android.base.contentprovider

import android.content.ContentResolver
import android.database.Cursor
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cby.android.base.R
import android.content.ContentValues
import android.content.ContentUris
import android.net.Uri
import android.os.Build
import android.widget.TextView
import androidx.annotation.RequiresApi
import java.lang.StringBuilder
import java.lang.reflect.Method


/**
 * @description: 子进程页面通过contentProvider进行数据操作，和方法调用
 *
 * @author ChenBaoyang
 * @date 2021/10/13 17:15
 */
class ContentProcessActivity : AppCompatActivity() {

    private val resolver: ContentResolver by lazy { contentResolver }

    private lateinit var showTv: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_content_process)
        showTv = findViewById(R.id.tv_show)
    }

    fun hasProvider():Boolean {
        val clz = ContentResolver::class.java;
        //获取方法
        //获取方法
        val printMethod: Method = clz.getMethod("acquireExistingProvider", Uri::class.java)
        //调用
        //调用
        val result:Any? = printMethod.invoke(resolver, Constant.CONTENT_URI)
        return result != null
    }

    fun onQuery(view: View) {
        if(!hasProvider()){
            return
        }
        // 获取tablename表中所有记录
        //val provider = resolver.acquireProvider()
        val sb = StringBuilder()
        val cursor: Cursor? = resolver.query(Constant.CONTENT_URI, null, null, null, null)
        if (cursor != null) {
            while (cursor.moveToNext()) {
                sb.append("id = ${cursor.getInt(0)}, name = ${cursor.getString(1)}，age = ${cursor.getString(2)}\n")
                Log.i(
                    "ContentTest",
                    "id=" + cursor.getInt(0).toString() + ", name=" + cursor.getString(1)
                )
            }
        }
        showTv.text = sb.toString()
    }

    fun onQuery2(view: View) {
        val sb = StringBuilder()
        val resultUri = ContentUris.withAppendedId(Constant.CONTENT_URI, 20)
        val cursor: Cursor? = resolver.query(resultUri, null, null, null, null)
        if (cursor != null) {
            while (cursor.moveToNext()) {
                sb.append("id = ${cursor.getInt(0)}, name = ${cursor.getString(1)}，age = ${cursor.getString(2)}\n")
                Log.i(
                    "ContentTest",
                    "id=" + cursor.getInt(0).toString() + ", name=" + cursor.getString(1)
                )
            }
        }
        showTv.text = sb.toString()
    }

    fun onQuery3(view: View) {
        val sb = StringBuilder()
        val args = arrayOf("1","100")
        val cursor: Cursor? = resolver.query(Constant.CONTENT_URI, null, "id in (?,?)", args, null)
        //val cursor: Cursor? = resolver.query(Constant.CONTENT_URI, null, "id between ? and ?", args, null)
        if (cursor != null) {
            while (cursor.moveToNext()) {
                sb.append("id = ${cursor.getInt(0)}, name = ${cursor.getString(1)}，age = ${cursor.getString(2)}\n")
                Log.i(
                    "ContentTest",
                    "id=" + cursor.getInt(0).toString() + ", name=" + cursor.getString(1)
                )
            }
        }
        showTv.text = sb.toString()
    }

    fun onInsert(view: View) {
        // 添加一条记录
        val values = ContentValues()
        values.put("name", "wang1")
        values.put("age", 28)
        resolver.insert(Constant.CONTENT_URI, values)
    }

    fun onUpdate(view: View) {
        // 把id为1的记录的name字段值更改新为zhang1
        // 把id为1的记录的name字段值更改新为zhang1
        val updateValues = ContentValues()
        updateValues.put("name", "zhang1")
        resolver.update(Constant.CONTENT_URI, updateValues, null, null)
    }

    private var test = false

    @RequiresApi(Build.VERSION_CODES.Q)
    fun onDoCall(view: View) {
        if(test) {
            val data = resolver.call(Constant.CONTENT_URI,
                    "test",
                    "1112",
                    null)
            showTv.text = data?.get("key").toString()
            test = false
        }else{
            val data = resolver.call(Constant.CONTENT_URI,
                "test2",
                "2222",
                null)
            showTv.text = data?.get("key").toString()
            test = true
        }

    }


}